package com.taiji.tvlauncher;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter
{
    /*myContext为上下文*/
    private Context context;
    private int focusPos;
    int textFocusColor = Color.rgb(255, 255, 255);
    int textUnfocusColor = Color.rgb(120, 120, 120);
    int textSize;
    private Integer[] imageFocus;
    private Integer[] imageUnfocus;
    private Integer[] text_id;
    int defaultFocusPos;

    // 这是图片资源ID的数组
    static class ViewHolder
    {
        ImageView image;
        TextView text;
    }

    /*构造方法*/
    public ImageAdapter(Context context, Integer[] imageFocus, Integer[] imageUnfocus, Integer[] textId, int textSize)
    {
        this.context = context;
        this.imageFocus = imageFocus;
        this.imageUnfocus = imageUnfocus;
        this.text_id = textId;
        this.textSize = textSize;
        /*传入一个Context，本例中传入的是GridViewTest */
    }

    /* 设置默认选择的item */
    public void setDefaultFocus(int pos)
    {
        defaultFocusPos = pos;
        focusPos = pos;
    }

    /*返回资源ID数组长度*/
    @Override
    public int getCount()
    {
        return imageFocus.length;
    }

    /*得到Item*/
    @Override
    public Object getItem(int position)
    {
        return position;
    }

    public void notifyDataSetChanged(int pos)
    {
        focusPos = pos;
        notifyDataSetChanged();
    }

    /*获取Items的ID*/
    @Override
    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return position;
    }

    /*获取要显示的View对象*/
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        ViewHolder vHolder = null; /*解决内存泄露问题*/
        //如果convertView对象为空则创建新对象，不为空则复用
        if (convertView == null)
        {
            convertView = View.inflate(context, R.layout.menu_item, null);

            RelativeLayout rl = (RelativeLayout) convertView.findViewById(R.id.item_relative);

            vHolder = new ViewHolder();
            vHolder.image = (ImageView) rl.findViewById(R.id.itemImage);

            vHolder.text = (TextView) rl.findViewById(R.id.itemText);
            convertView.setTag(vHolder);
        }
        else
        {
            vHolder = (ViewHolder) convertView.getTag();
        }
        vHolder.image.setImageResource(imageFocus[position]);
        vHolder.image.setScaleType(ScaleType.CENTER);

        vHolder.text.setText(text_id[position]);
        vHolder.text.setTextSize(textSize);

        //image.setScaleType(ScaleType.CENTER_CROP);
        //image.setAdjustViewBounds(true);
        //image.setBackground(getResources().getDrawable(R.drawable.item_focus_frame));
        if (focusPos != position)
        {
            vHolder.image.setImageResource(imageUnfocus[position]);
            vHolder.text.setTextColor(textUnfocusColor);
        }
        else
        {
            vHolder.image.setImageResource(imageFocus[position]);
            vHolder.text.setTextColor(textFocusColor);
            //image.setBackground(getResources().getDrawable(R.drawable.frame_focus));
            //image.setBackground(getResources().getDrawable(R.drawable.item_focus_frame));
        }
        return convertView;
    }
}